<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="机动车驾驶证" prop="motorVehicleDriverLicense">
        <el-input
          v-model="queryParams.motorVehicleDriverLicense"
          placeholder="请输入机动车驾驶证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['driver:attachments:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['driver:attachments:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['driver:attachments:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['driver:attachments:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="attachmentsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="附件记录ID" align="center" prop="id" />
      <el-table-column label="关联的司机ID" align="center" prop="driverId" />
      <el-table-column
        label="手机运营商"
        align="center"
        prop="mobileOperator"
      />
      <el-table-column
        label="公司标识"
        align="center"
        prop="companyIdentifier"
      />
      <el-table-column
        label="注册地行政区代码"
        align="center"
        prop="registrationDistrictCode"
      />
      <el-table-column
        label="驾驶员出生日期"
        align="center"
        prop="driverBirthDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.driverBirthDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="国籍" align="center" prop="nationality" />
      <el-table-column
        label="驾驶员民族"
        align="center"
        prop="driverEthnicity"
      />
      <el-table-column
        label="驾驶证编号"
        align="center"
        prop="driverLicenseNumber"
      />
      <el-table-column
        label="驾驶员婚姻状况"
        align="center"
        prop="driverMaritalStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.marital_status"
            :value="scope.row.driverMaritalStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="驾驶员外语能力"
        align="center"
        prop="driverForeignLanguageAbility"
      />
      <el-table-column
        label="驾驶员学历"
        align="center"
        prop="driverEducation"
      />
      <el-table-column
        label="户口登记机关名称"
        align="center"
        prop="householdRegistrationAuthority"
      />
      <el-table-column
        label="户口住址或常住地址"
        align="center"
        prop="householdAddress"
      />
      <el-table-column
        label="驾驶员通信地址"
        align="center"
        prop="driverContactAddress"
      />
      <el-table-column
        label="驾驶员照片文件编号"
        align="center"
        prop="driverPhotoFileNumber"
      />
      <el-table-column
        label="机动车驾驶证"
        align="center"
        prop="motorVehicleDriverLicense"
      />
      <el-table-column
        label="机动车驾驶证扫描文件编号"
        align="center"
        prop="motorVehicleDriverLicenseScanFileNumber"
      />
      <el-table-column
        label="准驾车型"
        align="center"
        prop="permittedDrivingType"
      />
      <el-table-column
        label="初次领取驾驶证日期"
        align="center"
        prop="firstDriverLicenseAcquisitionDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(
              scope.row.firstDriverLicenseAcquisitionDate,
              "{y}-{m}-{d}"
            )
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="驾驶证有效期起"
        align="center"
        prop="driverLicenseValidityStartDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.driverLicenseValidityStartDate, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="驾驶证有效期止"
        align="center"
        prop="driverLicenseValidityEndDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.driverLicenseValidityEndDate, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否专职驾驶员"
        align="center"
        prop="isFullTimeDriver"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.full_time"
            :value="scope.row.isFullTimeDriver"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="是否在驾驶员黑名单内"
        align="center"
        prop="isInDriverBlacklist"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.black_status"
            :value="scope.row.isInDriverBlacklist"
          />
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceType" />
      <el-table-column
        label="驾驶员合同"
        align="center"
        prop="driverContractSigningCompany"
      />
      <el-table-column
        label="紧急情况联系人"
        align="center"
        prop="emergencyContactPerson"
      />
      <el-table-column
        label="紧急情况联系人电话"
        align="center"
        prop="emergencyContactPhone"
      />
      <el-table-column
        label="紧急情况联系人通信地址"
        align="center"
        prop="emergencyContactAddress"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:attachments:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['driver:attachments:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改司机信息附件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联的司机ID" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入关联的司机ID" />
        </el-form-item>
        <el-form-item label="手机运营商" prop="mobileOperator">
          <el-input
            v-model="form.mobileOperator"
            placeholder="请输入手机运营商"
          />
        </el-form-item>
        <el-form-item label="公司标识" prop="companyIdentifier">
          <el-input
            v-model="form.companyIdentifier"
            placeholder="请输入公司标识"
          />
        </el-form-item>
        <el-form-item label="注册地行政区代码" prop="registrationDistrictCode">
          <el-input
            v-model="form.registrationDistrictCode"
            placeholder="请输入注册地行政区代码"
          />
        </el-form-item>
        <el-form-item label="驾驶员出生日期" prop="driverBirthDate">
          <el-date-picker
            clearable
            v-model="form.driverBirthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择驾驶员出生日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入国籍" />
        </el-form-item>
        <el-form-item label="驾驶员民族" prop="driverEthnicity">
          <el-input
            v-model="form.driverEthnicity"
            placeholder="请输入驾驶员民族"
          />
        </el-form-item>
        <el-form-item label="驾驶证编号" prop="driverLicenseNumber">
          <el-input
            v-model="form.driverLicenseNumber"
            placeholder="请输入驾驶证编号"
          />
        </el-form-item>
        <el-form-item label="驾驶员婚姻状况" prop="driverMaritalStatus">
          <el-radio-group v-model="form.driverMaritalStatus">
            <el-radio
              v-for="dict in dict.type.marital_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="驾驶员外语能力"
          prop="driverForeignLanguageAbility"
        >
          <el-input
            v-model="form.driverForeignLanguageAbility"
            placeholder="请输入驾驶员外语能力"
          />
        </el-form-item>
        <el-form-item label="驾驶员学历" prop="driverEducation">
          <el-input
            v-model="form.driverEducation"
            placeholder="请输入驾驶员学历"
          />
        </el-form-item>
        <el-form-item
          label="户口登记机关名称"
          prop="householdRegistrationAuthority"
        >
          <el-input
            v-model="form.householdRegistrationAuthority"
            placeholder="请输入户口登记机关名称"
          />
        </el-form-item>
        <el-form-item label="户口住址或常住地址" prop="householdAddress">
          <el-input
            v-model="form.householdAddress"
            placeholder="请输入户口住址或常住地址"
          />
        </el-form-item>
        <el-form-item label="驾驶员通信地址" prop="driverContactAddress">
          <el-input
            v-model="form.driverContactAddress"
            placeholder="请输入驾驶员通信地址"
          />
        </el-form-item>
        <el-form-item label="驾驶员照片文件编号" prop="driverPhotoFileNumber">
          <el-input
            v-model="form.driverPhotoFileNumber"
            placeholder="请输入驾驶员照片文件编号"
          />
        </el-form-item>
        <el-form-item label="机动车驾驶证" prop="motorVehicleDriverLicense">
          <el-input
            v-model="form.motorVehicleDriverLicense"
            placeholder="请输入机动车驾驶证"
          />
        </el-form-item>
        <el-form-item
          label="机动车驾驶证扫描文件编号"
          prop="motorVehicleDriverLicenseScanFileNumber"
        >
          <el-input
            v-model="form.motorVehicleDriverLicenseScanFileNumber"
            placeholder="请输入机动车驾驶证扫描文件编号"
          />
        </el-form-item>
        <el-form-item
          label="初次领取驾驶证日期"
          prop="firstDriverLicenseAcquisitionDate"
        >
          <el-date-picker
            clearable
            v-model="form.firstDriverLicenseAcquisitionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择初次领取驾驶证日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="驾驶证有效期起"
          prop="driverLicenseValidityStartDate"
        >
          <el-date-picker
            clearable
            v-model="form.driverLicenseValidityStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择驾驶证有效期起"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="驾驶证有效期止"
          prop="driverLicenseValidityEndDate"
        >
          <el-date-picker
            clearable
            v-model="form.driverLicenseValidityEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择驾驶证有效期止"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否专职驾驶员" prop="isFullTimeDriver">
          <el-radio-group v-model="form.isFullTimeDriver">
            <el-radio
              v-for="dict in dict.type.full_time"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否在驾驶员黑名单内" prop="isInDriverBlacklist">
          <el-radio-group v-model="form.isInDriverBlacklist">
            <el-radio
              v-for="dict in dict.type.black_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-input v-model="form.serviceType" placeholder="请输入服务类型" />
        </el-form-item>
        <el-form-item label="驾驶员合同" prop="driverContractSigningCompany">
          <el-input
            v-model="form.driverContractSigningCompany"
            placeholder="请输入驾驶员合同"
          />
        </el-form-item>
        <el-form-item label="紧急情况联系人" prop="emergencyContactPerson">
          <el-input
            v-model="form.emergencyContactPerson"
            placeholder="请输入紧急情况联系人"
          />
        </el-form-item>
        <el-form-item label="紧急情况联系人电话" prop="emergencyContactPhone">
          <el-input
            v-model="form.emergencyContactPhone"
            placeholder="请输入紧急情况联系人电话"
          />
        </el-form-item>
        <el-form-item
          label="紧急情况联系人通信地址"
          prop="emergencyContactAddress"
        >
          <el-input
            v-model="form.emergencyContactAddress"
            placeholder="请输入紧急情况联系人通信地址"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAttachments,
  getAttachments,
  delAttachments,
  addAttachments,
  updateAttachments,
} from "@/api/driver/attachments";

export default {
  name: "Attachments",
  dicts: ["marital_status", "full_time", "black_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 司机信息附件表格数据
      attachmentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverId: null,
        mobileOperator: null,
        companyIdentifier: null,
        registrationDistrictCode: null,
        driverBirthDate: null,
        nationality: null,
        driverEthnicity: null,
        driverLicenseNumber: null,
        driverMaritalStatus: null,
        driverForeignLanguageAbility: null,
        driverEducation: null,
        householdRegistrationAuthority: null,
        householdAddress: null,
        driverContactAddress: null,
        driverPhotoFileNumber: null,
        motorVehicleDriverLicense: null,
        motorVehicleDriverLicenseScanFileNumber: null,
        permittedDrivingType: null,
        firstDriverLicenseAcquisitionDate: null,
        driverLicenseValidityStartDate: null,
        driverLicenseValidityEndDate: null,
        isFullTimeDriver: null,
        isInDriverBlacklist: null,
        serviceType: null,
        driverContractSigningCompany: null,
        emergencyContactPerson: null,
        emergencyContactPhone: null,
        emergencyContactAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询司机信息附件列表 */
    getList() {
      this.loading = true;
      listAttachments(this.queryParams).then((response) => {
        this.attachmentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        driverId: null,
        mobileOperator: null,
        companyIdentifier: null,
        registrationDistrictCode: null,
        driverBirthDate: null,
        nationality: null,
        driverEthnicity: null,
        driverLicenseNumber: null,
        driverMaritalStatus: null,
        driverForeignLanguageAbility: null,
        driverEducation: null,
        householdRegistrationAuthority: null,
        householdAddress: null,
        driverContactAddress: null,
        driverPhotoFileNumber: null,
        motorVehicleDriverLicense: null,
        motorVehicleDriverLicenseScanFileNumber: null,
        permittedDrivingType: null,
        firstDriverLicenseAcquisitionDate: null,
        driverLicenseValidityStartDate: null,
        driverLicenseValidityEndDate: null,
        isFullTimeDriver: null,
        isInDriverBlacklist: null,
        serviceType: null,
        driverContractSigningCompany: null,
        emergencyContactPerson: null,
        emergencyContactPhone: null,
        emergencyContactAddress: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加司机信息附件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAttachments(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改司机信息附件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAttachments(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttachments(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除司机信息附件编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAttachments(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "driver/attachments/export",
        {
          ...this.queryParams,
        },
        `attachments_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
